﻿using System.Collections.Generic;
using System.Security.Claims;
using BilbilBLL;
using Common;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Modles.DTO;
using Modles.Models;

namespace bilbil.Controllers
{
    /// <summary>
    /// 主页功能
    /// </summary>
    /// 


    [Route("api/[controller]/[action]")]
    [ApiController]
    public class HomeController : ControllerBase
    {
        //初始化Server层
        HomeServer homeServer = new HomeServer();


        /// <summary>
        /// 打印轮播图表按id降序排序
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetCarousel()
        {
            Response_DTO<List<Carousel_DTO>> response = homeServer.GetCarousel();
            return new JsonResult(new
            {
                code = 200,
                msg = response.Message,
                data = response.Conten
            });
        }
        /// <summary>
        /// 打印轮播图表2按id降序排序
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetCarouselTwo()
        {
            ResponseTool response = homeServer.GetCarouselTwo();
            return new JsonResult(response);
        }
        /// <summary>
        /// 获取视频类型
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetVideoType()
        {
            Response_DTO<List<VideoType_DTO>> response = homeServer.GetVideoType();
            return new JsonResult(new
            {
                code = response.StatuCode,
                msg = response.Message,
                data = response.Conten
            });
        }
        /// <summary>
        /// 获取主页视频信息（时间降序排序，提供分页功能）
        /// </summary>
        /// <param name="model"></param>
        /// <param name="TotalCount"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetVideoInfo(int page, int limit)
        {
            //声明TotalCount变量，用于记录查询出来的数据总和数给前端使用
            int TotalCount;
            ResponseTool res = homeServer.GetVideoInfo(page, limit, out TotalCount);
            //将数据响应给前端
            return new JsonResult(new
            {
                code = res.code,
                msg = res.Message,
                count = TotalCount,
                data = res.data
            });
        }
        /// <summary>
        /// 打印视频信息表，按播量排序，仅输出前100个视频信息
        /// </summary>
        /// <param name="model"></param>
        /// <param name="TotalCount"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetVideoInfoSort()
        {
            ResponseTool res = homeServer.GetVideoInfoSort();
            //将数据响应给前端
            return new JsonResult(new
            {
                code = res.code,
                msg = res.Message,
                data = res.data
            });
        }
        /// <summary>
        /// 根据前端传过来的视频标题(title)，打印视频信息表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetVideoInfoBytitle([FromQuery] StudentQuery_DTO model)
        {
            //声明TotalCount变量，用于记录查询出来的数据总和数给前端使用
            int TotalCount;

            ResponseTool res = homeServer.GetVideoInfoBytitle(model, out TotalCount);
            //将数据响应给前端
            return new JsonResult(new
            {
                code = res.code,
                msg = res.Message,
                count = TotalCount,
                data = res.data
            });
        }

        /// <summary>
        /// 将前端发送过来的搜索记录保存到搜索记录表，判断是否保存有该搜索记录，如果有就增加搜索次数
        /// </summary>
        /// <param name="dto"></param>
        [HttpPost]
        public void AddSearchForRecord(SearchForRecord_DTO dto)
        {
            homeServer.AddSearchForRecord(dto);
        }

        /// <summary>
        /// 将搜索记录表的数据按降序打印出来仅打印前10条
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetSearchForRecords()
        {
            Response_DTO<List<SearchForRecord>> res = homeServer.GetSearchForRecords();
            return new JsonResult(new
            {
                code = res.StatuCode,
                msg = res.Message,
                data = res.Conten
            });
        }

        /// <summary>
        /// 获取观看已视频的历史记录
        /// </summary>
        /// <param name="UserInfoId"></param>
        /// <returns></returns>
        /// 
        [HttpGet]
        [Authorize]
        public IActionResult GetVideoHistory()
        {
            int userInfoId = Convert.ToInt32(User.FindFirst(ClaimTypes.NameIdentifier)?.Value);
            ResponseTool res = homeServer.GetVideoHistory(userInfoId);
            return new JsonResult(new
            {
                code = res.code,
                msg = res.Message,
                data = res.data
            });
        }
    }
}
